/**
 * 验证token
 */

const jwt = require('jsonwebtoken');
const setting = require('../config/setting.js');

const verify = {
  setToKen(_id) {
    return new Promise((resolve) => {
      let token = jwt.sign({ _id }, setting.token.secret, {
        expiresIn: setting.token.expiresIn,
      });
      resolve(token);
    });
  },
  getToken(token) {
    return new Promise((resolve, reject) => {
      if (!token.split(' ').length) {
        reject({ error: 'The token value is empty' });
      } else {
        let data = jwt.verify(token.split(' ')[1], setting.token.secret);
        resolve(data);
      }
    });
  },
};

module.exports = verify;
